package com.da.javatest.interview.session2;

import java.util.concurrent.Callable;
import java.util.concurrent.FutureTask;
import java.util.concurrent.TimeUnit;

/**
 * @author chenlida
 * @date 2020/4/1 19:23
 * @description
 */
public class Test20Callable {
    public static void main(String[] args) throws Exception {
        Callable<Integer> callable = () -> {
            TimeUnit.SECONDS.sleep(2);
            System.out.println(Thread.currentThread().getName() + " come in");
            return 1024;
        };
        FutureTask futureTask = new FutureTask<>(callable);
        new Thread(futureTask, "AA").start();
        // 共用FutureTask只会执行一次
        new Thread(futureTask, "BB").start();
        //System.out.println(futureTask.get());
        //System.out.println(callable.call());
        int result1 = 100;
        System.out.println("main线程*********");

        //while (!futureTask.isDone()) {
        //}

        // get()会阻塞线程
        Integer result2 = (Integer)futureTask.get();
        System.out.println(result1 + result2);
    }
}
